#/usr/bin/env python
#
# Author:   Joshua Canter (jcanter@clemson.edu)
# Revision: 14 July 2008
#
#   Copyright 2007-2008 Clemson University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# This material is based upon work supported under a National Science
# Foundation Graduate Research Fellowship. Any opinions, findings,
# conclusions or recommendations expressed in this publication are those of
# the author(s) and do not necessarily reflect the views of the National
# Science Foundation.

import socket
import struct

def wol(address, broadcast):
   '''WOL- Wake on lan awakens a machine with MAC address in address, which is
      a six part tuple. WOL broadcasts over the range of broadcast, a tuple of
      an address with type string  and an integer port.'''
   sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)


   payload = struct.pack('B',0xff)
   for i in range(0,5):
      payload += struct.pack('B',0xff)
   for i in range(0,16):
      payload += struct.pack('BBBBBB', address[0],address[1],address[2], \
         address[3],address[4],address[5])

   sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
   sock.sendto(payload, broadcast)
   sock.close()


def main():
   while True:
      wol((127,4,16,34,23,43), ('127.0.0.255',1025))

if __name__ == "__main__":
   main()